<template>
  <div class="hello">
    <div class="home">
      <div class="search">
        <el-form :inline="true" :model="formInline" ref="formInline"
          style="margin: auto;text-align: center;margin-top: 20px;display: flex;justify-content: space-between;">
          <el-form-item label="项目名称" style="width:220px;">
            <el-input placeholder="请输入内容" suffix-icon="el-icon-search" v-model="formInline.name" style="width: 120px;">
            </el-input>
          </el-form-item>
          <el-form-item label="供应商名称" style="width:220px;">
            <el-input placeholder="请输入内容" suffix-icon="el-icon-search" v-model="formInline.artificialPerson"
              style="width: 120px;">
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="supplierSearch"
              style="background-color: #47ACF4;margin-left: 620px;">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="clean('formInline')"
              style="margin-left: 12px;background: transparent; color: #47ACF4;">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="main">
        <div>
          <div style="margin-top: 16px;padding-bottom: 16px;border-bottom: 2px solid #DCDFE6;">
            <span style="margin: 16px 24px;font-family: PingFangSC-Medium;
font-weight: 600;
font-size: 16px;
color: #303133;
line-height: 24px;">外委列表</span>
          </div>
          <div class="top">
            <div style="text-align: right;margin-top: 24px;">
              <el-button type="primary" @click="newOutsourcing" style="margin-left: 30px;background-color: #47ACF4;"
                v-if="editColumn.delete">新增外委申请</el-button>
              <el-button type="primary" @click="exportExcel"
                style="margin-left: 12px;background: transparent; color: #47ACF4;margin-right: 24px;">导出</el-button>
            </div>
          </div>
        </div>
        <div style="margin: 12px 24px;">
          <el-table :data="tableData" :cell-style="tableStyle" @selection-change="handleSelectionChange"
            :header-cell-style="headerCellStyle" style="width: 100%">
            <!-- <el-table-column type="selection" width="55">
            </el-table-column> -->
            <el-table-column v-for="( column, index ) of  columns " :key="index" :prop="column.prop"
              :label="column.label" :fixed="column.fixed" :width="column.width">
            </el-table-column>
            <el-table-column v-if="editColumn" fixed="right" label="操作" width="120">
              <template slot-scope="scope">
                <el-button v-if="editColumn.view" @click="view(scope.row)" type="text" size="small">详情</el-button>
                <el-button v-if="editColumn.edit" @click="editRow(scope.row)" type="text" size="small">编辑</el-button>
                <el-button v-if="editColumn.delete" @click="deleteItem(scope.row)" type="text"
                  size="small">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <el-pagination background layout="prev, pager, next,sizes,total" :total=total :page-size=pageSize
          :page-sizes="[10, 15, 20]" :current-page.sync="pageNow" style="text-align:center;align-self: flex-end;"
          @prev-click="changePage" @current-change="changePage" @next-click="changePage" @size-change="sizeChange">
        </el-pagination>
      </div>
    </div>
    <el-dialog :visible.sync="editProjectShow" width="80%">
      <EditProject @close-dialog="closeDialog" :supplierInfo="supplierInfo" />
    </el-dialog>
    <el-dialog title="批量导入外委申请" :visible.sync="importFlag">
      <el-form ref="form" label-width="100px">
        <el-form-item label="相关附件">
          <el-upload :auto-upload="false" action="javascript:void(0)" :on-remove="handleRemove"
            :file-list="importFileList" :on-change="handleChange">
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <div style="text-align: right;">
        <el-button type="primary" style="height: 30px;" @click="downMB">模板下载</el-button>
        <el-button type="primary" style="height: 30px;" @click="importProject">确定导入</el-button>
        <el-button style="height: 30px;" @click="importFlag = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>


<script>
import Table from '@/components/NormalTable'
import { MessageBox } from 'element-ui';
import { getOutsourcingList } from "@/api/outsourcingManagement/outsourcing";
import { downloadGet } from '@/utils/request'
import EditProject from './editOutsourcing.vue'
export default {
  components: {
    Table,
    EditProject
  },
  dicts: ['grade'],
  data() {
    return {
      deleteTips: '',
      editColumn: { view: true, edit: false, delete: false },
      editProjectShow: false,
      importFlag: false,
      importFileList: [],
      createFlag: false,
      selected: 0,
      fileList: [],
      pageNow: 1,
      supplierInfo: {},
      headerCellStyle: {
        "background-color": '#F5F7FA',
        'text-align': 'center',
        'font-family': 'PingFangSC-Medium',
        'font-weight': 500,
        'font-size': '14px',
        'color': '#606266',
        'line-height': '22px'
      },
      tableStyle: {
        "text-align": 'center',
      },
      formInline: {
        pageNum: 1,
        pageSize: 5,
        name: '',
        credential: '',
        artificialPerson: '',
        grade: ''
      },
      pageSize: 5,
      total: 0,
      options: [],
      columns: [
        { prop: 'index', label: '序号', width: '', fixed: false },
        { prop: 'projectName', label: '项目名称', width: '100', fixed: false },
        { prop: 'projectAmount', label: '项目资金总额', width: '200', fixed: false },
        { prop: 'projectContent', label: '项目工作内容与任务量', width: '140', fixed: false },
        { prop: 'content', label: '计划外委工作内容与任务量', width: '100', fixed: false },
        { prop: 'amount', label: '计划外委金额', width: '100', fixed: false },
        { prop: 'bank', label: '计划外委金额占总金额比例', width: '130', fixed: false },
        { prop: 'enterpriseName', label: '外委供应商名称', width: '120', fixed: false },
        { prop: 'number', label: '人数', width: '120', fixed: false },
        { prop: 'reason', label: '外委原因', width: '100', fixed: false },
        { prop: 'status', label: '状态', width: '150', fixed: false },
        { prop: 'grade', label: '评分', width: '120', fixed: false },
      ],
      tableData: [],
      headerList: [
        {
          name: '全部项目',
          icon: 'menu',
          count: '20',
          style: 'color:#47ACF4;font-size: 18px;',
          green: true,
          status: ''
        },
        {
          name: '进行中',
          icon: 's-claim',
          count: '18',
          style: 'color:#F9CD43;font-size: 18px;',
          green: false,
          status: '2'
        },
        {
          name: '已完成',
          icon: 'document-checked',
          count: '3',
          style: 'color:#2ABF73;;font-size: 18px;font-weight: 600;',
          green: false,
          status: '3'
        },
        {
          name: '已归档',
          icon: 'check',
          count: '3',
          style: 'color:rgba(255, 186, 0, 1);;font-size: 18px;font-weight: 600;',
          green: false,
          status: '4'
        }
      ]
    }
  },
  mounted() {
    this.userRole = this.$store.state.userInfo.userRoleKeyList
    this.userRole.forEach(item => {
      if (item == 'zgb_scgly' || item == 'admin') {
        this.editColumn.edit = true
        this.editColumn.delete = true
        this.createFlag = true
      }
    });
    // this.setDict();
    this.loadTable();
    this.pageNow = 1
    // this.getDeptList().then(res => {
    //   this.options = res.data[0].children
    //   // console.log("dept", this.options);
    // });

  },
  methods: {
    loadTable() {
      this.formInline.pageNum = 1;
      this.formInline.orderByColumn = "createTime"
      this.formInline.isAsc = 'desc'
      getOutsourcingList(this.formInline).then(res => {
        this.tableData = res.rows;
        this.tableData.forEach((item, index) => {
          item.index = index + 1
        });
        this.total = res.total
      });

    },
    getDict(dictName) {
      let dict = this.$store.state.dictNew[dictName]
      return dict
      // console.log('task', dict)
    },
    //字典
    setDict() {
      getDictList().then(res => {
        this.$store.state.dictNew = res.data;
      })
    },
    //查询项目
    supplierSearch() {
      console.log(this.formInline)
      this.formInline.pageNum = 1;
      this.formInline.orderByColumn = "createTime"
      this.formInline.isAsc = 'desc'
      getOutsourcingList(this.formInline).then(res => {
        this.tableData = res.rows;
        this.tableData.forEach((item, index) => {
          item.index = index + 1
        });
        this.total = res.total
      });
    },
    handleSelectionChange() {

    },
    statusBtn(status, index) {
      this.selected = index
      this.formInline.status = status
      this.supplierSearch()
    },
    //清空
    clean(formName) {
      this.formInline = {
        name: '',
        startTime: '',
        endTime: '',
        leadDeptId: ''
      };
      this.$refs[formName].resetFields();
      this.loadTable();
      this.pageNow = 1
    },
    //查看详情
    view(row) {
      let id = this.tableData[row.index - 1].id
      this.$router.push('/supplierDetails?id=' + id);
    },
    editRow(row) {
      this.editProjectShow = true
      this.supplierInfo = row
      this.supplierInfo.id = this.tableData[row.index - 1].id
      console.log('this.projectInfo', this.supplierInfo)

    },
    closeDialog() {
      console.log("123321")
      this.editProjectShow = false;
      this.loadTable();
      this.pageNow = 1
    },
    exportExcel() {
      downloadGet('enterprise/export', this.formInline, `供应商列表.xlsx`)
    },
    //删除项目
    deleteItem(row) {
      MessageBox.confirm("您确定要删除" + row.name + "这项数据？", '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 用户点击了确定按钮，执行对应操作
        let id = this.tableData[row.index - 1].id
        console.log(id)
        deleteSupplier(id).then(res => {
          if (res.code == 200) {
            alert("删除成功")
            this.loadTable();
            this.pageNow = 1
          }
        })
      }).catch(() => {
        // 用户点击了取消按钮，取消对应操作
      });
    },
    //新建项目
    newOutsourcing() {
      this.$router.push('/newOutsourcing');
    },
    openImportProject() {
      this.importFlag = true
    },
    downMB() {
      window.open(this.downBaseUrl + '/xmgl/template/%E4%BE%9B%E5%BA%94%E5%95%86%E5%AF%BC%E5%85%A5%E6%A8%A1%E7%89%88.xlsx');
    },
    importProject() {
      let formData = new FormData();
      formData.append("file", this.fileList[0].raw, "");
      importSupplier(formData).then(res => {
        this.$modal.msgSuccess("导入成功！");
        this.importFlag = false
        this.loadTable()
        console.log(res)
      })
    },
    //附件移除
    handleRemove(file, fileList) {
      this.fileList = fileList;
    },
    // 材料改变
    handleChange(file, fileList) {

      const res = this.beforeAvatarUpload(file, fileList);
      if (!res || fileList.length >= 2) {
        fileList.shift();
      }
      this.fileList = fileList;
    },
    // 上传前控制
    beforeAvatarUpload(file, fileList) {
      const isLt2M = file.size / 1024 / 1024 < 100;
      if (!isLt2M) {
        this.$message.error(`上传图片大小不能超过100MB!`);
      }
      return isLt2M;
    },
    // 定义一个函数用于转换字典值为标签
    convertDictValueToLabel(dictValue, dictName) {
      const labelMap = {};
      let dict = this.$store.state.dictNew[dictName]
      dict.forEach(item => {
        labelMap[item.value] = item.label
      });
      let label = '';
      if (labelMap.hasOwnProperty(dictValue)) {
        label += labelMap[dictValue] + ' ';
      }
      // console.log('label', label.trim())
      return label.trim(); // 去除首尾空格
    },
    //翻页
    changePage(pageNum) {
      this.formInline.pageNum = pageNum
      this.formInline.orderByColumn = "createTime"
      this.formInline.isAsc = 'desc'
      getSupplierList(this.formInline).then(res => {
        this.tableData = res.rows;
        this.tableData.forEach((item, index) => {
          item.index = index + 1
        });
        this.total = res.total
      });
    },
    sizeChange(size) {
      this.formInline.pageSize = size
      this.loadTable();
    },

  }
}
</script>

<style scoped lang="scss">
.home {
  display: flex;
  flex-direction: column;
  width: 100wh;
  /* height: 100vh; */
  padding: 0px;
  background-color: #F5f7fa;
  // background-color: red;
  font-weight: 500;

  .top {
    // display: flex;
    flex-direction: row;
    text-align: right;
    align-items: right;
    justify-content: space-between;
    width: 100wh;
    height: 8vh;
    padding: 0px;
    /* background-color: rgb(0, 255, 55); */

    .top-item1 {
      display: flex;
      flex-direction: column;
      margin-left: 15px;
      padding-left: 15px;
      // border-left: 2px solid #DCDFE6;
    }



    .top-item {
      display: flex;
      flex-direction: column;
      margin-left: 15px;
      padding-left: 15px;
      border-left: 2px solid #DCDFE6;
      // transition: border-bottom 0.3s ease;

      .greenPoint {
        background-color: #2ABF73;
        height: 10px;
        width: 10px;
        margin-right: 5px;
        margin-top: 19px;
        border-radius: 5px;
        margin-left: 10px;
      }
    }

    .test1:hover+.bottomLine {
      background-color: #DCDFE6;
    }

    .selected {
      background-color: skyblue;
    }

    .top-item:hover {
      // background-color: skyblue;
    }

  }

  .search {
    // width: 100%;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    background-color: #FFFFFF;
    margin: 12px 24px;
  }

  .main {
    margin-top: 10px;
    background-color: #FFFFFF;
    margin: 12px 24px;
  }
}
</style>

<style lang="scss">
.el-form-item__label {
  font-family: PingFangSC-Regular;
  font-weight: 400;
  font-size: 14px;
  color: #606266;
}
</style>
